﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HangerSupport
{
	public partial class DlgCableTrayWeightCal : System.Windows.Forms.Form
	{
		public DlgCableTrayWeightCal(List<SystemLine> _lines)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.userSetting = new UserSetting();
			this.lines = _lines;
			base.Icon = Resource.YArchitechIcon;
			this.setDefault();
			this.calCableTrayWeight();
			this.calTotal();
			this.isInit = false;
		}

		private void setDefault()
		{
			string value = this.userSetting.GetValue("CableTrayWeightCal", "fillingRate");
			this.hyTextBox4.Text = ((value == "") ? "50" : value);
			this.fillingRate = Convert.ToDouble(this.hyTextBox4.Text);
			this.hyTextBox2.Text = ((this.userSetting.GetValue("CableTrayWeightCal", "addWeight") == "") ? "0" : this.userSetting.GetValue("CableTrayWeightCal", "addWeight"));
			this.hyTextBox5.Text = ((this.userSetting.GetValue("CableTrayWeightCal", "addRatio") == "") ? "10" : this.userSetting.GetValue("CableTrayWeightCal", "addRatio"));
			this.hyTextBox3.Text = ((this.userSetting.GetValue("CableTrayWeightCal", "itemizeRatio") == "") ? "1.35" : this.userSetting.GetValue("CableTrayWeightCal", "itemizeRatio"));
		}

		private void calTotal()
		{
			this.fillingRate = ((this.hyTextBox4.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox4.Text));
			this.addWeight = ((this.hyTextBox2.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox2.Text));
			this.addRatio = ((this.hyTextBox5.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox5.Text));
			this.itemizeRatio = ((this.hyTextBox3.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox3.Text));
			this.userSetting.WriteValue("CableTrayWeightCal", "fillingRate", this.fillingRate.ToString());
			this.userSetting.WriteValue("CableTrayWeightCal", "addWeight", this.addWeight.ToString());
			this.userSetting.WriteValue("CableTrayWeightCal", "addRatio", this.addRatio.ToString());
			this.userSetting.WriteValue("CableTrayWeightCal", "itemizeRatio", this.itemizeRatio.ToString());
			this.hyTextBox6.Text = string.Format("{0:F}", Math.Round((this.totalCableTrayWeight + this.addWeight + this.totalElectricCable) * (1.0 + this.addRatio / 100.0) * 9.8 * this.itemizeRatio, 2));
		}

		private void calCableTrayWeight()
		{
			this.totalCableTrayWeight = 0.0;
			this.totalElectricCable = 0.0;
			foreach (SystemLine systemLine in this.lines)
			{
				double num = 0.0;
				this.totalCableTrayWeight += this.getCableTrayAndElectric(systemLine.Pipes.Cast<CableTray>().ToList<CableTray>(), ref num);
				this.totalElectricCable += num;
			}
			this.hyTextBox1.Text = string.Format("{0:F}", Math.Round(this.totalCableTrayWeight, 2));
		}

		private double getCableTrayAndElectric(List<CableTray> cts, ref double ElectricCable)
		{
			double num = 0.0;
			foreach (CableTray ct in cts)
			{
				double num2 = 0.0;
				num += this.getSingleCableTrayAndElectric(ct, ref num2);
				ElectricCable += num2;
			}
			return num;
		}

		private double getSingleCableTrayAndElectric(CableTray ct, ref double ElectricCable)
		{
			string a = ct.GetParameter(Autodesk.Revit.DB.BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString();
			string name = ct.Name;
			string type = "";
			double num = NumbericTools.feetTomm(ct.GetParameter(Autodesk.Revit.DB.BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
			ElectricCable = Math.Pow(this.fillingRate / 100.0, 2.0) * (NumbericTools.feetTomm(ct.Width) / 10.0) * (NumbericTools.feetTomm(ct.Height) / 10.0) * 8.9 / 1000.0 / 1.1 * (num / 10.0);
			bool flag = a == "带配件的电缆桥架";
			if (flag)
			{
				bool flag2 = name == "强电桥架";
				if (flag2)
				{
					type = "托盘式";
				}
				else
				{
					bool flag3 = name == "槽式电缆桥架" || name.Contains("槽盒") || name == "弱电桥架" || name == "消防桥架" || name == "默认";
					if (flag3)
					{
						type = "槽盒式";
					}
					else
					{
						bool flag4 = name == "梯级式电缆桥架";
						if (flag4)
						{
							type = "梯级式";
						}
					}
				}
			}
			else
			{
				bool flag5 = a == "无配件的电缆桥架";
				if (flag5)
				{
					bool flag6 = name == "单轨电缆桥架" || name == "金属丝网电缆桥架" || name == "默认";
					if (flag6)
					{
						type = "槽盒式";
					}
				}
				else
				{
					type = "梯级式";
				}
			}
			double density = this.getDensity(type, ct.Width, ct.Height);
			return num / 1000.0 * density;
		}

		private double getDensity(string type, double width, double height)
		{
			double num = 0.0;
			width = NumbericTools.feetTomm(width);
			height = NumbericTools.feetTomm(height);
			List<CableTrayFormat> list = new List<CableTrayFormat>();
			DensitySpecificationManage densitySpecificationManage = new DensitySpecificationManage();
			list = (from f in densitySpecificationManage.GetSpecificationsByType(type)
			orderby f.Height
			select f).ToList<CableTrayFormat>();
			List<CableTrayFormat> list2 = (from f in list
			where f.Width >= width
			select f).ToList<CableTrayFormat>();
			bool flag = list2 == null || list2.Count < 1;
			double result;
			if (flag)
			{
				bool flag2 = list.Count <= 0;
				if (flag2)
				{
					result = num;
				}
				else
				{
					num = list.Last<CableTrayFormat>().density;
					result = num;
				}
			}
			else
			{
				list2 = (from f in list2
				orderby f.Width
				select f).ToList<CableTrayFormat>();
				double baseWidth = list2.First<CableTrayFormat>().Width;
				list2 = (from f in list2
				where f.Width == baseWidth
				orderby f.Height
				select f).ToList<CableTrayFormat>();
				List<CableTrayFormat> list3 = (from f in list2
				where f.Height >= height
				select f).ToList<CableTrayFormat>();
				bool flag3 = list3 == null || list3.Count < 1;
				if (flag3)
				{
					num = (from f in list2
					orderby f.Height
					select f).Last<CableTrayFormat>().density;
				}
				else
				{
					num = (from f in list3
					orderby f.Height
					select f).First<CableTrayFormat>().density;
				}
				result = num;
			}
			return result;
		}

		private bool isMatchFormat(double height, CableTrayFormat ctf)
		{
			return height <= ctf.Height;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void hyTextBox2_TextChanged(object sender, EventArgs e)
		{
			bool flag = !this.isInit;
			if (flag)
			{
				System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
				bool flag2 = textBox.Name.Equals("hyTextBox4");
				if (flag2)
				{
					this.fillingRate = ((textBox.Text.Trim() == "") ? 0.0 : Convert.ToDouble(textBox.Text));
					this.calCableTrayWeight();
				}
				this.calTotal();
			}
		}

		private List<SystemLine> lines = null;

		private bool isInit = true;

		private double totalCableTrayWeight = 0.0;

		private double totalElectricCable = 0.0;

		private double addWeight = 0.0;

		private double addRatio = 10.0;

		private double itemizeRatio = 1.35;

		private double fillingRate = 50.0;

		private UserSetting userSetting = null;
	}
}
